const app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainInfo: {
      train_id: 0,
      title: "",// '标题',
      image: "",// '',
      brief: "",//'简介',
      syllabus: "",//'课程大纲',
      notes: "",//'报名须知',
      start_time: "2018-12-10",//'培训开始时间',
      end_time: "2018-12-10",//'结束时间',
      province_id: 0,//'省份',
      city_id: 0,//'城市',
      county_id: 0,// '区县',
      address: "",// '',
      train_type_id: 0,//'名师|学者',
      teacher_id: 0,//'讲师ID',
      price: 0,//'0.00',
      qrcode: "",//'二维码',
      status: 1,//'1'      
    },

    customItem: '全部',
    region: ['湖南省', '长沙市', '岳麓区'],
    index: 0,
    type_name: "",
    train_type_id: 0,
    trainTypeList: [],
    userInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var typeList = wx.getStorageSync("trainType")
    var trainTypeList = new Array();
    for (var item in typeList[options.train_type_id].sub) {
      var itm = typeList[options.train_type_id].sub[item];
      trainTypeList.push(itm)
    }
    this.setData({
      trainTypeList: trainTypeList,
      userInfo: wx.getStorageSync("userInfo")
    })
    if (options.train_id) {
      this.setData({
        'trainInfo.train_id': options.train_id
      });
      this.getTrain();
    }

  },
  bindTrainTypeChange: function (e) {
    var selectType = this.data.trainTypeList[e.detail.value]
    console.log(selectType)
    this.setData({
      index: e.detail.value,
      type_name: selectType.type_name,
      train_type_id: selectType.train_type_id
    })
    console.log(e, this.data.index)
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  getTrain: function () {
    let that = this;
    util.request(api.TrainDetail, { train_id: that.data.trainInfo.train_id }).then(res => {
      this.setData({
        trainInfo: res.data.trainInfo
      })
      wx.hideLoading();
    }).catch(() => {
      wx.hideLoading();
    });
  },
  formSubmit: function (e) {
    var data = e.detail.value;
    data.image = this.data.trainInfo.image;
    var teacherInfo = wx.getStorageSync("teacherInfo")
    data.teacher_id = teacherInfo.teacher_id;
    var selectType = this.data.trainTypeList[this.data.index]
    data.train_type_id = selectType.train_type_id;
    data.start_time = this.data.trainInfo.start_time;
    data.end_time = this.data.trainInfo.end_time;
    data.image = this.data.trainInfo.image;
    if (this.data.trainInfo.train_id > 0) {
      data.train_id = this.data.trainInfo.train_id
    }
    console.log(data)
    util.request(api.TrainSubmit, data, "POST").then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '发布成功',
          duration: 2000,
          success: function () {
            wx.redirectTo({
              url: '/pages/user/fbindex/index',
            })
          }
        })

      }

    });
  },
  chooseImage: function (e) {
    var that = this;
    var field = e.currentTarget.dataset.field
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.ImageUpload,
          filePath: tempFilePaths[0],
          header: {
            'Content-Type': 'application/json'
          },
          name: 'file',
          formData: {
            token: wx.getStorageSync("token")
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            if (data.code == 200) {
              if (field == "image") {
                that.setData({
                  'trainInfo.image': data.data.url
                });
              }

            }
          }
        })
      }
    })
  },
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e)
    var trainInfo = this.data.trainInfo;
    trainInfo[e.currentTarget.dataset.name] = e.detail.value
    this.setData({
      trainInfo: trainInfo
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})